package pl.smath.expression.model.presentation;

import pl.smath.expression.model.Term;

public class FactorofTerm extends PresentationTerm {

	public enum RelationType{
		FACTOR_OF, NOT_FACTOR_OF;
	};
	
	/** Lewe wyrazenie */
	private final Term leftTerm;
	/** Prawe wyrazenie */
	private final Term rightTerm;
	/** typ */
	private RelationType relationType;

	/**
	 * constructor
	 * @param type
	 * @param left
	 * @param right
	 */
	public FactorofTerm(String type, Term left, Term right) {

		super(type);
		relationType = string2type(type);
		this.leftTerm = left;
		this.rightTerm = right;
	}

	public RelationType getRelationType() {
		return relationType;
	}

	public Term getLeftTerm() {
		return leftTerm;
	}

	public Term getRightTerm() {
		return rightTerm;
	}

	/**
	 * Convertuje postac tekstowa relacji na typ
	 * @param type
	 * @return
	 */
	private static RelationType string2type(String type) {
		
		if(type.compareTo("factorof") == 0)
			return RelationType.FACTOR_OF;
		else if(type.compareTo("notfactorof") == 0)
			return RelationType.NOT_FACTOR_OF;
		
		return null;
	}

	/**
	 * Sprawdzenie czy 2 wartości bezwgledne wyrazenia sa sobie równowazne.
	 */
	public boolean isAbsTheSame(Term term){
		if (!(term instanceof FactorofTerm))
			return false;
		
		FactorofTerm fot = (FactorofTerm)term; 
		
		if (fot.getRelationType() != getRelationType())
			return false;
		
		if (!fot.getLeftTerm().isAbsTheSame(getLeftTerm())  ||  !fot.getRightTerm().isAbsTheSame(getRightTerm()))
			return false;
		
		return true;
	}
	
	public String toString(){
		if (relationType == RelationType.FACTOR_OF)
			return getLeftTerm().toString() + "|" + getRightTerm().toString();
		else
			return getLeftTerm().toString() + "!|" + getRightTerm().toString();
	}
}
